package Offer2018.Compare;

import com.sun.tools.corba.se.idl.constExpr.Or;

public class Goods implements Comparable<Goods> {

    private String name;
    private double price;

    public Goods(String name, double price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    //指明商品比较大小的方式：按照价格从低到高,再按照产品名称从低到高排序
    @Override
    public int compareTo(Goods o) {
        //方式一
        if (this.price > o.price) {
            return 1;
        } else if (this.price < o.price) {
            return -1;
        } else {
            //价格相等,按照产品名称从低到高排序
            return this.name.compareTo(o.name);
        }

        //方式二
        //return Double.compare(this.price,o.price);
    }
}
